package cn.shopifymall.studying.jdbc;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @Author 孤风雪影
 * @Email gitee.com/efairy520
 * @Date 2022/5/3 20:20
 * @Version 1.0
 */
public class Base64Test {
    public static void main(String[] args) throws Exception {
        // try {
        //     //注册驱动，指明JDBC的实现类
        //     Class.forName("com.mysql.jdbc.Driver");
        // } catch (ClassNotFoundException e) {
        //     e.printStackTrace();
        // }

        String base64Code = "";
        decoderBase64File(base64Code, "D:\\Develop\\IdeaProject\\doc2\\bpmn.zip");
    }

    public static String encodeBase64File(String path) throws Exception {
        File file = new File(path);
        FileInputStream inputFile = new FileInputStream(file);
        byte[] buffer = new byte[(int) file.length()];
        inputFile.read(buffer);
        inputFile.close();
        return new BASE64Encoder().encode(buffer);
    }

    public static void decoderBase64File(String base64Code, String targetPath)
            throws Exception {
        byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
        FileOutputStream out = new FileOutputStream(targetPath);
        out.write(buffer);
        out.close();
    }

}
